<%@page import="com.dilean.impl.model.ConversationImpl"%>
<%@page import="com.dilean.api.dao.ConversationDAO"%>
<%@page import="com.dilean.api.model.Conversation"%>
<%@page import="java.util.HashSet"%>
<%@page import="com.dilean.api.model.AttributeListValue"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%-- imports --%>
<%@page import="com.dilean.api.dao.AttributeTypeDAO"%>
<%@page import="com.dilean.impl.model.AttributeTypeImpl"%>
<%@page import="com.dilean.api.model.AttributeType"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.model.AttributeListValueImpl"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="java.math.BigInteger"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumber"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueListMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        
        <%
            
            Person pers1 = new PersonImpl();
            Person pers2 = new PersonImpl();
            final int pers1Id = Integer.parseInt(request.getParameter("id"));
            final int pers2Id = Integer.parseInt(request.getParameter("id2"));
            pers1 = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithConversations(pers1Id);
            pers2 = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithConversations(pers2Id);
            Collection<Conversation> conversations1 = pers1.getConversations();
            Collection<Conversation> conversations2 = pers2.getConversations();
            
             Conversation conversation = new ConversationImpl();
             conversation.setName("1_1");
            
            for(Conversation c1:conversations1){
                Collection<Person> convPersons = AplicationContextHolder.getInstance().getContext().getBean(ConversationDAO.class).findByIdWithPersons(c1.getId()).getPersons();
                for(Conversation c2:conversations2){
                    if((c1.getId()==c2.getId())&&(convPersons.size()==2)){
                        
                        conversation=c1;
                    }
                }
            }
            
            
            
            if(conversation.getName().compareTo("1_1")==0){
                conversation.setName(pers1.getName()+" "+pers2.getName());
                conversation.addPerson(pers1);
                conversation.addPerson(pers2);
                AplicationContextHolder.getInstance().getContext().getBean(ConversationDAO.class).save(conversation);
            }
           


        %>     
        
        
        
        
    </body>
</html>
